package com.city.java.abnormal_records.service.impl;

import com.city.java.abnormal_records.mapper.AbnormalRecordMapper;
import com.city.java.abnormal_records.pojo.entity.AbnormalRecords;
import com.city.java.base.response.JsonResult;
import com.city.java.base.response.StatusCode;
import com.city.java.abnormal_records.service.AbnormalRecordService;
import com.city.java.base.utils.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 异常记录服务实现类
 */
@Slf4j
@Service
public class AbnormalRecordServiceImpl implements AbnormalRecordService {

    @Autowired
    private AbnormalRecordMapper abnormalRecordMapper;

    @Override
    public JsonResult addAbnormalRecord(AbnormalRecords abnormalRecord) {
        log.debug("准备添加异常记录: {}", abnormalRecord);

        // 参数校验
        if (abnormalRecord == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录信息不能为空");
        }

        if (abnormalRecord.getAbnormalType() == null || abnormalRecord.getAbnormalType().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常类型不能为空");
        }

        if (abnormalRecord.getReason() == null || abnormalRecord.getReason().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常原因不能为空");
        }

        // 设置创建时间
        if (abnormalRecord.getCreatedAt() == null) {
            abnormalRecord.setCreatedAt(LocalDateTime.now());
        }

        try {
            abnormalRecordMapper.insertAbnormalRecord(abnormalRecord);
            log.info("异常记录添加成功: 异常记录ID={}", abnormalRecord.getAbnormalId());
            return JsonResult.ok();
        } catch (Exception e) {
            log.error("添加异常记录失败: 异常类型={}", abnormalRecord.getAbnormalType(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "添加异常记录失败");
        }
    }

    @Override
    public JsonResult getAbnormalRecordById(Long abnormalId) {
        log.debug("根据ID查询异常记录: {}", abnormalId);

        // 参数校验
        if (abnormalId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        try {
            AbnormalRecords abnormalRecord = abnormalRecordMapper.selectById(abnormalId);
            if (abnormalRecord != null) {
                log.info("查询异常记录成功: ID={}", abnormalId);
                return JsonResult.ok(abnormalRecord);
            } else {
                log.warn("异常记录不存在: ID={}", abnormalId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录不存在");
            }
        } catch (Exception e) {
            log.error("查询异常记录失败: ID={}", abnormalId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录失败");
        }
    }

    @Override
    public JsonResult getAbnormalRecordsByAbnormalType(String abnormalType) {
        log.debug("根据异常类型查询异常记录列表: {}", abnormalType);

        // 参数校验
        if (abnormalType == null || abnormalType.trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常类型不能为空");
        }

        try {
            List<AbnormalRecords> abnormalRecords = abnormalRecordMapper.selectByAbnormalType(abnormalType);
            log.info("查询异常类型记录列表成功: 异常类型={}, 记录数量={}", abnormalType, abnormalRecords.size());
            return JsonResult.ok(abnormalRecords);
        } catch (Exception e) {
            log.error("查询异常类型记录列表失败: 异常类型={}", abnormalType, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录列表失败");
        }
    }

    @Override
    public JsonResult getAllAbnormalRecords() {
        log.debug("查询所有异常记录列表");

        try {
            List<AbnormalRecords> abnormalRecords = abnormalRecordMapper.selectAll();
            log.info("查询所有异常记录列表成功: 记录数量={}", abnormalRecords.size());
            return JsonResult.ok(abnormalRecords);
        } catch (Exception e) {
            log.error("查询所有异常记录列表失败", e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "查询异常记录列表失败");
        }
    }

    @Override
    public JsonResult deleteAbnormalRecordById(Long abnormalId) {
        log.debug("根据ID删除异常记录: {}", abnormalId);

        // 参数校验
        if (abnormalId == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        try {
            int result = abnormalRecordMapper.deleteById(abnormalId);
            if (result > 0) {
                log.info("删除异常记录成功: ID={}", abnormalId);
                return JsonResult.ok();
            } else {
                log.warn("删除异常记录失败，异常记录不存在: ID={}", abnormalId);
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录不存在");
            }
        } catch (Exception e) {
            log.error("删除异常记录失败: ID={}", abnormalId, e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "删除异常记录失败");
        }
    }

    @Override
    public JsonResult updateAbnormalRecord(AbnormalRecords abnormalRecord) {
        log.debug("更新异常记录信息: {}", abnormalRecord);

        // 参数校验
        if (abnormalRecord == null || abnormalRecord.getAbnormalId() == null) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常记录ID不能为空");
        }

        if (abnormalRecord.getAbnormalType() != null && abnormalRecord.getAbnormalType().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常类型不能为空");
        }

        if (abnormalRecord.getReason() != null && abnormalRecord.getReason().trim().isEmpty()) {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "异常原因不能为空");
        }

        try {
            // 检查异常记录是否存在
            AbnormalRecords existingAbnormalRecord = abnormalRecordMapper.selectById(abnormalRecord.getAbnormalId());
            if (existingAbnormalRecord == null) {
                log.warn("更新异常记录失败，异常记录不存在: ID={}", abnormalRecord.getAbnormalId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "异常记录不存在");
            }

            int result = abnormalRecordMapper.updateById(abnormalRecord);
            if (result > 0) {
                log.info("更新异常记录成功: ID={}", abnormalRecord.getAbnormalId());
                return JsonResult.ok();
            } else {
                log.warn("更新异常记录失败: ID={}", abnormalRecord.getAbnormalId());
                return new JsonResult(StatusCode.OPERATION_FAILED, "更新异常记录失败");
            }
        } catch (Exception e) {
            log.error("更新异常记录失败: ID={}", abnormalRecord.getAbnormalId(), e);
            return new JsonResult(StatusCode.OPERATION_FAILED, "更新异常记录失败");
        }
    }

    @Override
    public PageVO<AbnormalRecords> select(AbnormalRecords abnormalRecords) {
        abnormalRecords.setPageNum(PageVO.build(abnormalRecords.getPageNum(), abnormalRecords.getPageSize()));
        List<AbnormalRecords> list = abnormalRecordMapper.select(abnormalRecords);
        Long totalPage = abnormalRecordMapper.getTotal(abnormalRecords);

        PageVO<AbnormalRecords> pageVO = new PageVO<>();
        pageVO.setList(list);
        pageVO.setTotalPage(totalPage);

        log.debug("查询异常记录列表成功: {}", pageVO);
        return pageVO;
    }
}
